use AlysDB

/**
Represent the complex numbers in tsql. 

Power of a RDBMS is that sets encode the multiplicative products that we
want to see, and the computatoionally intensive part is the
set product that is optimized in SQL Server. 

-- notation? make tables all Caps? 

20131027, Ryan Price
**/ 


declare @kat int = 0,  @kfinal int = 0
declare @x table ( x float ) 
declare @y table ( y float ) 
declare @z table ( x float, y float ) 
declare @stp float= 0.0   -- does scientific notation work? 
-- is pi in the tsql? 

-- initialize all of them
set @kfinal = 101	
set @stp = 0.01
print N' stepsize = ' + convert(varchar(5), @stp)
print N' points   = ' + convert(varchar(5), @kfinal)




set @kat = 1	

while @kat  < @kfinal + 1
BEGIN

insert into  @x
values ((@kat-1)*@stp)


insert into  @y
values ((@kat-1)*@stp)

set @kat += 1
END

-- check the result here
select x from @x 
select y from @y
-- both should be 0.00 to 1.00


-- loop implicity to select statement makes problem computation very fast
-- the inner product, outer join or cross join is better
-- ** need to see this work


-- compute results.  Not stored, just result sets. 
-- get all pos unit square in space C
/**
select 
x as 'Re(z)',
y as 'Im(z)'
from
@x cross join @y
**/


-- until decide to carry row labels, have to copy @x into Re(z)
insert into @z ( x ) 
select x from @x


/**
-- cvar add/ subtract 
update @z 
set x = Z.x+Z.x, y = Y.y + Y.y 
from 
@z as Z inner join @y as Y 
on Z.x = Y.y
**/


/**
-- cvar multiply 
-- ( x + iy )( x + iy ) = xx + ixy + ixy - yy
update @z 
set x = Z.x*Z.x - Y.y*Y.y, y = 2*Z.x*Y.y
from @z as Z inner join @y as Y
on Z.x = Y.y
**/


--/**
-- cvar divide
-- ( x + iy )/( x + iy ) = (xx + yy)/ (xx + yy) + i(xy - yx)/ (xx + yy)
update @z 
set x = (Z.x*Z.x + Y.y*Y.y)/ (Z.x*Z.x + Y.y*Y.y), 
y =  (Z.x*Y.y - Y.y*Z.x)/ (Z.x*Z.x + Y.y*Y.y)
from @z as Z inner join @y as Y
on Z.x = Y.y
where Z.x*Y.Y>0
--**/

select x,y from @z



delete @z -- reset the table @z




